package com.ruoyi.comment.controller;

import java.lang.reflect.Array;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.comment.domain.Comment;
import com.ruoyi.comment.service.ICommentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 求职论坛Controller
 * 
 * @author ruoyi
 * @date 2024-12-11
 */
@RestController
@RequestMapping("/comment/comment")
public class CommentController extends BaseController
{
    @Autowired
    private ICommentService commentService;


    /**
     * 查询求职论坛列表
     */
    @PreAuthorize("@ss.hasPermi('comment:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Comment comment) {
        List<Comment> list = commentService.selectCommentList(comment);
        List<Comment> rootComments = new ArrayList<>();
        Map<Long, List<Comment>> childCommentsMap = new HashMap<>();

        for (Comment comment1 : list) {
            Long parentId = comment1.getParentId();
            if (parentId == null) {
                rootComments.add(comment1);
            } else {
                childCommentsMap.computeIfAbsent(parentId, k -> new ArrayList<>()).add(comment1);
            }
        }

        for (Comment rootComment : rootComments) {
            Long rootCommentId = rootComment.getId();
            List<Comment> childComments = childCommentsMap.get(rootCommentId);
            if (childComments != null) {
                rootComment.setChildren(childComments);
            }
        }

        return pagination(rootComments);
    }


    /**
         * 新增求职论坛
         */
    @PreAuthorize("@ss.hasPermi('comment:comment:add')")
    @Log(title = "求职论坛", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Comment comment)
    {

        comment.setUsername(SecurityUtils.getUsername());
        comment.setTime(DateUtils.getTime());
        return toAjax(commentService.insertComment(comment));
    }


    /**
     * 删除求职论坛
     */
    @PreAuthorize("@ss.hasPermi('comment:comment:remove')")
    @Log(title = "求职论坛", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(commentService.deleteCommentByIds(ids));
    }
}
